from pydantic import BaseModel, Field, EmailStr
from typing import Optional, List
from app.schemas.base import BaseSchema

class UserBase(BaseModel):
    """用户基础模式"""
    username: str = Field(..., description="用户名")
    name: str = Field(..., description="姓名")
    email: Optional[str] = Field(None, description="邮箱")
    phone: Optional[str] = Field(None, description="手机号")
    avatar: Optional[str] = Field(None, description="头像")
    status: bool = Field(True, description="状态：True-启用，False-禁用")
    department_id: Optional[int] = Field(None, description="部门ID")

class UserCreate(UserBase):
    """创建用户模式"""
    password: str = Field(..., description="密码")
    role_ids: List[int] = Field(default=[], description="角色ID列表")

class UserUpdateAdmin(UserBase):
    """管理员更新用户模式"""
    username: Optional[str] = Field(None, description="用户名")
    name: Optional[str] = Field(None, description="姓名")
    password: Optional[str] = Field(None, description="密码")
    role_ids: Optional[List[int]] = Field(None, description="角色ID列表")

class UserInDB(UserBase, BaseSchema):
    """数据库中的用户模式"""
    pass

class UserOut(UserInDB):
    """用户输出模式"""
    department_name: Optional[str] = Field(None, description="部门名称")
    roles: List[dict] = Field(default=[], description="角色列表")

class UserLogin(BaseModel):
    """用户登录模式"""
    username: str = Field(..., description="用户名")
    password: str = Field(..., description="密码")

class Token(BaseModel):
    """令牌模式"""
    access_token: str = Field(..., description="访问令牌")
    token_type: str = Field("bearer", description="令牌类型")

class TokenData(BaseModel):
    """令牌数据模式"""
    username: Optional[str] = None

class ChangePassword(BaseModel):
    """修改密码模式"""
    old_password: str = Field(..., description="旧密码")
    new_password: str = Field(..., description="新密码")
    confirm_password: str = Field(..., description="确认密码")

class UserUpdate(BaseModel):
    """用户更新模式（个人资料）"""
    name: Optional[str] = Field(None, description="姓名")
    email: Optional[str] = Field(None, description="邮箱")
    phone: Optional[str] = Field(None, description="手机号")
